/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tbenglish_client.form;

import bllfactory.SBLLFactory;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import model.ClientModel;

/**
 *
 * @author Mrkupi
 */
public class friendRequest extends javax.swing.JFrame {

    /**
     * Creates new form friendRequest
     */
    public friendRequest() {
        initComponents();
        setResizable(false);
        setLocation( 400, 200 );
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               dispose();
            }
        });
        loadRequestFriend();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbName = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        btAccept = new javax.swing.JButton();
        btDecline = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tbName.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name"
            }
        ));
        jScrollPane1.setViewportView(tbName);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("FRIEND REQUESTS");

        btAccept.setText("Accept");
        btAccept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAcceptActionPerformed(evt);
            }
        });

        btDecline.setText("Decline");
        btDecline.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeclineActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(129, 129, 129)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(111, 111, 111)
                        .addComponent(btAccept)
                        .addGap(40, 40, 40)
                        .addComponent(btDecline)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(25, 25, 25)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAccept)
                    .addComponent(btDecline))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAcceptActionPerformed
        int row     =   this.tbName.getSelectedRow();
        int colum   =   this.tbName.getSelectedColumn();
        String name =   (String)this.tbName.getValueAt(row, colum);
        try 
        {
            // insert new relationship
            SBLLFactory.getInstance().getRelation().insertNewRelationShip(  ClientModel.getInstance().getAccount().getUserID(), name );
            // delete request
            SBLLFactory.getInstance().getRelation().deleteRequestFriend( ClientModel.getInstance().getAccount().getUserID(), name );
            loadRequestFriend();
        } catch (RemoteException ex) 
        {
            Logger.getLogger(friendRequest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btAcceptActionPerformed

    private void btDeclineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeclineActionPerformed
        int row     =   this.tbName.getSelectedRow();
        int colum   =   this.tbName.getSelectedColumn();
        String name =   (String)this.tbName.getValueAt(row, colum);
        try 
        {
            // delete request
            SBLLFactory.getInstance().getRelation().deleteRequestFriend( ClientModel.getInstance().getAccount().getUserID(), name );
            loadRequestFriend();
        } catch (RemoteException ex) 
        {
            Logger.getLogger(friendRequest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btDeclineActionPerformed

    private void loadRequestFriend()
    {
        try
        {
            ArrayList<String> strs  =   SBLLFactory.getInstance().getRelation().getRequestedFriend( ClientModel.getInstance().getAccount().getUserID() );
            DefaultTableModel dtm   =   new DefaultTableModel();
            Vector listColums   =   new Vector();
            listColums.add( "Name" );
            dtm.setColumnIdentifiers( listColums );

            for( int i = 0; i < strs.size(); ++i )
            {
                String s  =   strs.get( i );
                Vector row          =   new Vector();
                row.add( s );
                dtm.addRow( row );
            }
            if( this.tbName != null )
            {
                this.tbName.setModel( dtm );
            }
            
        } catch (RemoteException ex) {
            Logger.getLogger(friendRequest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAccept;
    private javax.swing.JButton btDecline;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbName;
    // End of variables declaration//GEN-END:variables
}
